package com.zego.wawaji.ui.center.invite;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.noble.library.basic.string.StringUtils;
import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.base.toast.PToast;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.third.share.ShareUtils;
import com.zego.wawaji.third.share.util.ShareEnum;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.bean.wawa.Wawa;
import com.zego.wawaji.ui.center.invite.bean.Invite;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;
import com.zego.wawaji.ui.dialog.share.ShareDialog;

/**
 * 邀请界面
 * Created by Su on 2017/11/27.
 */
public class InviteAct extends BaseAppCompatActivity implements ShareDialog.OnShareClickListener, RequestComplete, View.OnClickListener {
    private TextView invite_daily_num, tv_share_circle, tv_share_friend, tv_share_content,
            tv_invite_suc, tv_invite_num, tv_invite_content, tv_send_content;
    private LinearLayout ll_box;
    private ImageView treasure_box;
    private Invite invite;
    private SpannableStringBuilder ssbuilder;
    private ForegroundColorSpan yellowSpan;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_invite_act);
        initView();
        initResultView();
    }

    private void initView() {
        setTitle("邀请好友");
        setBackView();
        ShareUtils.getInstance().reqShareData(null);

        invite_daily_num = (TextView) findViewById(R.id.invite_daily_num);
        tv_share_circle = (TextView) findViewById(R.id.tv_share_circle);
        tv_share_friend = (TextView) findViewById(R.id.tv_share_friend);
        tv_share_content = (TextView) findViewById(R.id.tv_share_content);
        tv_invite_suc = (TextView) findViewById(R.id.tv_invite_suc);
        tv_invite_num = (TextView) findViewById(R.id.tv_invite_num);
        tv_invite_content = (TextView) findViewById(R.id.tv_invite_content);
        tv_send_content = (TextView) findViewById(R.id.tv_send_content);
        fv_text = (TextView) findViewById(R.id.fv_text);
        treasure_box = (ImageView) findViewById(R.id.treasure_box);
        ll_box = (LinearLayout) findViewById(R.id.ll_box);
        ll_box.setOnClickListener(this);
        ModuleMgr.getWawaMgr().reqInvitePage(this);
        findViewById(R.id.invite_btn).setOnClickListener(v -> PickerDialogUtil.showShareDialog(InviteAct.this));
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqInvitePage) {
            if (!response.isOk()) return;

            invite = (Invite) response.getBaseData();
            invite_daily_num.setText("今日邀请" + invite.getInviteDailyNum() + "位");
            tv_share_circle.setText(invite.getShareCircle());
            tv_share_friend.setText(invite.getShareFriends());
            tv_share_content.setText(invite.getShareContent());
            tv_send_content.setText(invite.getSendContent());

            String str = invite.getInviteSuccess();
            String expr = invite.getInviteSuccessRed();
            if (ssbuilder == null)
                ssbuilder = new SpannableStringBuilder(invite.getInviteSuccess());
            if (yellowSpan == null)
                yellowSpan = new ForegroundColorSpan(getResources().getColor(R.color.text_red));
            ssbuilder.setSpan(yellowSpan, StringUtils.getBeforeNoFlag(str, expr).length(),
                    StringUtils.getBeforeWithFlag(str, expr).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv_invite_suc.setText(ssbuilder);

            tv_invite_num.setText("总邀请" + invite.getInviteNum() + "位");
            tv_invite_content.setText(invite.getInviteContent());
            if (invite.getInviteBox() == 0) {
                boxOpened();
            }
            return;
        }

        if (response.getUrlParam() == UrlParam.reqInviteBox) {
            if (!response.isOk()) {
                PToast.showShort(response.getMsg());
                return;
            }
            wawa = new Wawa();
            wawa.parseJson(response.getResString());
            setShowAnimation(synth_suc);
            invite.setInviteBox(0);
            boxOpened();
        }
    }

    private void boxOpened() {
        fv_text.setText("已打开");
        treasure_box.setBackgroundResource(R.drawable.treasure_box02);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_box:
                if (invite == null) {
                    PToast.showShort("数据请求出错，请退出重试");
                    return;
                }

                if (invite.getInviteBox() == -1) {
                    PToast.showShort("不满足打开宝箱条件");
                    return;
                }

                if (invite.getInviteBox() == 0) {
                    PToast.showShort("您已打开过该宝箱");
                    return;
                }
                ModuleMgr.getWawaMgr().reqInviteBox(this);
                break;
        }
    }

    // ================================== 开宝箱结果 ====================================
    private RelativeLayout synth_suc;
    private ImageView synth_img;
    private TextView synth_remark, fv_text;
    private Wawa wawa;

    private void initResultView() {
        synth_suc = (RelativeLayout) findViewById(R.id.synth_suc);
        synth_img = (ImageView) findViewById(R.id.synth_img);
        synth_remark = (TextView) findViewById(R.id.synth_remark);
        synth_suc.setOnClickListener(this);   // 防点击透传

        findViewById(R.id.synth_close).setOnClickListener(v -> setHideAnimation(synth_suc));
    }

    /**
     * View渐隐动画效果
     */
    public void setHideAnimation(final View view) {
        if (view == null) return;

        view.startAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha_fade_out));
        view.getAnimation().setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    /**
     * View渐现动画效果
     */
    public void setShowAnimation(final View view) {
        if (view == null) return;

        if (wawa != null) {
            ImageLoader.loadCenterCrop(this, wawa.getImg(), synth_img, 0, 0);
            synth_remark.setText("获得娃娃：" + wawa.getName());
        }

        view.setVisibility(View.INVISIBLE);
        view.startAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha_fade_in));
        view.getAnimation().setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    public void shareWeiXin() {
        ShareUtils.getInstance().shareData(this, ShareEnum.WEIXIN);
    }

    @Override
    public void shareWXCircle() {
        ShareUtils.getInstance().shareData(this, ShareEnum.WEIXIN_FRIENDS);
    }

    @Override
    public void shareLink() {
        ShareUtils.getInstance().shareData(this, ShareEnum.COPY);
    }

    @Override
    public void shareQQ() {
        ShareUtils.getInstance().shareData(this, ShareEnum.QQ);
    }

    @Override
    public void shareQQGroup() {

    }

    @Override
    public void shareQQZone() {
        ShareUtils.getInstance().shareData(this, ShareEnum.QQ_ZONE);
    }

    @Override
    public void shareWeiBo() {
        ShareUtils.getInstance().shareData(this, ShareEnum.WEIBO);
    }
}
